package com.demo.impl.controller;

import com.demo.api.user.domain.TestLimit;
import com.demo.api.user.domain.TestUser;
import com.demo.api.user.service.LimitService;
import com.demo.api.user.service.MDSLimitService;
import com.demo.api.user.service.SeataTccLocalService;
import com.demo.api.user.service.UserService;
import com.demo.impl.service.OpenNewUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Date;

/**
 *
 * @author
 * @since
 */
@RestController
@Slf4j
public class Test001 {

  @Reference(version = "1.0.0", check = false)
  private UserService userService;

  @Reference(version = "1.0.0", check = false)
  private LimitService limitService;

  @Reference(version = "1.0.0", check = false)
  private MDSLimitService mdsLimitService;

  @Reference(version = "1.0.0", check = false)
  private SeataTccLocalService seataTccLocalService;

  @Autowired
  private OpenNewUserService openNewUserService;

  @GetMapping("/test")
  public String addUser(){
    log.info("接收到addUser请求");
    TestUser testUser = new TestUser();
    testUser.setFullName("1");
    testUser.setIdno("2");
    testUser.setSex("3");
    try {
      userService.addUser(testUser);
    } catch (Exception e){
      log.error("{}",e);
      return "Exception";
    }
    return "Okay";
  }

  @GetMapping("/limit")
  public String addLimit(){
    log.info("接收到addLimit请求");
    TestLimit testLimit = new TestLimit();
    testLimit.setIdno("2");
    testLimit.setLimitAmount(new BigDecimal(100));
    testLimit.setDt(new Date());
    try {
      limitService.addLimit(testLimit);
    } catch (Exception e){
      log.error("{}",e);
      return "Exception";
    }
    return "Okay";
  }

  @GetMapping("/seataCM")
  public String seataTest(){
    log.info("接收到 seataTest 请求");

    try {
      openNewUserService.addLimitCommit();
    } catch (Exception e){
      log.error("{}",e);
      return "Exception";
    }
    return "seataTest succ";
  }

  @GetMapping("/seataRB")
  public String seataTestRB(){
    log.info("接收到 seataTest 请求");

    try {
      openNewUserService.addLimitRollback();
    } catch (Exception e){
      log.error("{}",e);
      return "Exception";
    }
    return "seataTest succ";
  }

  @GetMapping("/AUL")
  public String addUserLimit(){
    try {
      log.info("接收到 addUserLimit 请求");
      openNewUserService.addUserAndLimit();
    } catch (Exception e){
      log.error("{}",e);
      return "Exception";
    }
    return "Okay";
  }

  @GetMapping("/SMDS")
  public String addUserAndLimitDivideDb(){
    try {
      log.info("接收到 addUserAndLimitDivideDb 请求");
      mdsLimitService.addUserAndLimitCommit();
    } catch (Exception e){
      log.error("{}",e);
      return "Exception";
    }
    return "Okay";
  }

  @GetMapping("/SMDS-rb")
  public String addUserAndLimitRb(){
    try {
      log.info("接收到 addUserAndLimitRb 请求");
      mdsLimitService.addUserAndLimitRollback();
    } catch (Exception e){
      log.error("{}",e);
      return "Exception";
    }
    return "Okay";
  }

  @GetMapping("/seata-tcc-local-ok")
  public String seataTccLocalOk(){
    try {
      log.info("接收到 seataTccLocalOk 请求");
      seataTccLocalService.addUserAndLimitCommit();
    } catch (Exception e){
      log.error("{}",e);
      return "Exception";
    }
    return "Okay";
  }

  @GetMapping("/seata-tcc-local-rb")
  public String seataTccLocalRb(){
    try {
      log.info("接收到 seataTccLocalRb 请求");
      seataTccLocalService.addUserAndLimitRollback();
    } catch (Exception e){
      log.error("{}",e);
      return "Exception";
    }
    return "Okay";
  }

}
